import Vue from "vue";
import VueRouter from "vue-router";


Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 配置路由懒加载
const Home = () => import('../views/home/Home');
const Login =() => import('../views/login/Login')
const Welcome = () => import('../views/welcome/Welcome')
const Desktop = () => import('../views/welcome/Desktop')
const UserInfo = () => import('../views/UserInfo/UserInfo')
const UserEdit = () => import('../views/UserInfo/UserEdit')
const UserAdd = () => import('../views/UserInfo/UserAdd')
const Log = () => import('../views/Log/Log')
const Department= () => import('../views/department/Department')
const DepartmentEdit= () => import('../views/department/DepartmentEdit')
const DepartmentAdd= () => import('../views/department/DepartmentAdd')
const Daily = () => import('../views/daily/Daily')
const DailyAdd = () => import('../views/daily/DailyAdd')
const DailyEdit = () => import('../views/daily/DailyEdit')
const Overtime = () => import('../views/overtime/Overtime')
const OvertimeAdd = () => import('../views/overtime/OvertimeAdd')
const OvertimeEdit = () => import('../views/overtime/OvertimeEdit')
const OvertimeAudit = () => import('../views/overtime/OvertimeAudit')

// 配置页面路径
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/desktop',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/desktop',
        component: Desktop
      },
      {
        path: '/userInfo',
        component: UserInfo
      },
      {
        path: '/userEdit',
        component: UserEdit
      },
      {
        path: '/addUser',
        component: UserAdd
      },
      {
        path: '/log',
        component: Log
      },
      {
        path: '/department',
        component: Department
      },
      {
        path: '/departmentEdit',
        component: DepartmentEdit
      },
      {
        path: '/departmentAdd',
        component: DepartmentAdd
      },
      {
        path: '/daily',
        component: Daily
      },
      {
        path: '/dailyAdd',
        component: DailyAdd
      },
      {
        path: '/dailyEdit',
        component: DailyEdit
      },
      {
        path: '/overtime',
        component: Overtime
      },
      {
        path: '/overtimeAdd',
        component: OvertimeAdd
      },
      {
        path: '/overtimeEdit',
        component: OvertimeEdit
      },{
        path: '/overtimeAudit',
        component: OvertimeAudit
      },
    ]
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'

})


// 股灾路由导航守卫
router.beforeEach((to, from, next) => {
  /**
   * to 将要访问的路径
   * from 代表从哪个路径跳转来的
   * next 为一个函数 表示放行
   */
  if (to.path === '/login') {
    return next()
  }
  const tokenStr = window.sessionStorage.getItem('token');
  if (tokenStr === null) {
    return next('/login')
  }
  next()
})

export default router
